package com.huawei.codecraft;
import java.io.*;
import java.util.*;

import static com.huawei.codecraft.Main.*;
import static java.lang.Math.*;
//3.15

public class Main {
    public static robot robot1= new robot();
    public static robot robot2= new robot();
    public static robot robot3= new robot();
    public static robot robot4= new robot();
    static double robotWeight_free = 4.05 * Math.PI;//空闲状态质量
    static double robotWeight_Work = 5.618 * Math.PI;//工作状态质量
    static double robotAngularAcceleration_Free = 50.00 / robotWeight_free;//空闲状态角加速度
    static double robotAngularAcceleration_Work = 50.00 / robotWeight_Work;//工作状态角加速度


    private static final Scanner inStream = new Scanner(System.in);

    private static final PrintStream outStream = new PrintStream(new BufferedOutputStream(System.out));

    public static char[][] map = new char[100][100];//地图数据
    public static int robot_number = 0;//记录机器人个数
    public static double[] robot_x = new double[4];//初始机器人位置x
    public static double[] robot_y = new double[4];//初始机器人位置y
    public static int workbench_number;//工作台数量
//工作台种类检索
    private static ArrayList<workbench> workbenches_1 = new ArrayList<>();
    private static ArrayList<workbench> workbenches_2 = new ArrayList<>();
    private static ArrayList<workbench> workbenches_3 = new ArrayList<>();
    private static ArrayList<workbench> workbenches_4 = new ArrayList<>();
    private static ArrayList<workbench> workbenches_5 = new ArrayList<>();
    private static ArrayList<workbench> workbenches_6 = new ArrayList<>();
    private static ArrayList<workbench> workbenches_7 = new ArrayList<>();
    private static ArrayList<workbench> workbenches_8 = new ArrayList<>();
    private static ArrayList<workbench> workbenches_9 = new ArrayList<>();

    public static ArrayList<Double> workbench1_x = new ArrayList<>();
    public static ArrayList<Double> workbench1_y = new ArrayList<>();
    public static ArrayList<Double> workbench2_x = new ArrayList<>();
    public static ArrayList<Double> workbench2_y = new ArrayList<>();
    public static ArrayList<Double> workbench3_x = new ArrayList<>();
    public static ArrayList<Double> workbench3_y = new ArrayList<>();
    public static ArrayList<Double> workbench4_x = new ArrayList<>();
    public static ArrayList<Double> workbench4_y = new ArrayList<>();
    public static ArrayList<Double> workbench5_x = new ArrayList<>();
    public static ArrayList<Double> workbench5_y = new ArrayList<>();
    public static ArrayList<Double> workbench6_x = new ArrayList<>();
    public static ArrayList<Double> workbench6_y = new ArrayList<>();
    public static ArrayList<Double> workbench7_x = new ArrayList<>();
    public static ArrayList<Double> workbench7_y = new ArrayList<>();
    public static ArrayList<Double> workbench8_x = new ArrayList<>();
    public static ArrayList<Double> workbench8_y = new ArrayList<>();
    public static ArrayList<Double> workbench9_x = new ArrayList<>();
    public static ArrayList<Double> workbench9_y = new ArrayList<>();
    //工作台编号检索
    static Map<Integer,workbench> integerworkbenchHashMap= new HashMap<>();
    static ArrayList<workbench> workbenchArr=new ArrayList<>();
    static Boolean workbenchInit= false;


//    public static String mapDir = "map.txt";//地图保存路径
//    public static File mapFile = new File(mapDir);
//    public static String isDir = "inStream.txt";//输入流保存路径
//    public static File isFile = new File(isDir);
//
//    public static BufferedWriter isWriter;//输入流写

    public static boolean frameFlag=false;//在第9000帧是设置为true 以便于isStream关闭

    public static void main(String[] args) throws IOException {
//        isFile.delete();
//        mapFile.delete();
//        isWriterInit();
        schedule();
    }

    private static void schedule() throws IOException {
        readMap();//读地图&初始化机器人与工作台位置信息
        outStream.println("OK");//告诉判题器地图初始化完毕
        outStream.flush();
        int frameID;


        while (inStream.hasNextLine()) {//遍历所有帧
            String line = inStream.nextLine();
            String[] parts = line.split(" ");
            frameID = Integer.parseInt(parts[0]);//获得每帧的第一行数据：帧数和当前钱数
            outStream.printf("%d\n", frameID);
//            readInStream(line, frameID);//写每一帧的第一行

            readUtilOK();//写每一帧的其他行


            Random r = new Random();


            int lineSpeed = r.nextInt() * 6;
            double angleSpeed = r.nextBoolean() ? 1 : -1 * r.nextDouble() * Math.PI;
//            for (int robotId = 0; robotId < 4; robotId++) {
//                outStream.printf("forward %d %d\n", robotId, lineSpeed);
//                outStream.printf("rotate %d %f\n", robotId, 0.0);
////                outStream.printf("buy %d\n", robotId);
////                outStream.printf("sell %d\n", robotId);
//
//            }
//            outStream.printf("forward %d %f\n", 0, robot1.getVelocity());
//            outStream.printf("rotate %d %f\n", 0, robot1.getAngular_velocity());
            if (frameID % 100 == 0) {
//                outStream.printf("buy %d\n", 1);
            }
            outStream.print("OK\n");
            outStream.flush();
        }
    }


    //debug
//                try {
//                    isWriter.write("\r\n");
//                    isWriter.write(robot1.getAngular_velocity()+" ");
//                    isWriter.write(robot1.getPosition_x()+" "+robot1.getPosition_y()+" "+robot1.getDirection());
//                    isWriter.write("\r\n");
//                    isWriter.flush();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }


//    parts[0] 所处工作台ID 没有就-1 整数 [0,n-1]
//    parts[1] 携带物品类型 没有就0  整数 [1,7]
//    parts[2] 时间价值系数 浮点 [0.8,1]
//    parts[3] 碰撞价值系数 浮点 [0.8,1]
//    parts[4] 角速度 浮点
//    parts[5] 线速度 X 浮点
//    parts[6] 线速度 y 浮点
//    parts[7] 朝向  浮点
//    parts[8] 坐标 x 浮点
//    parts[9] 坐标 y 浮点


    private static void robot1Move(robot robot1,String line) throws IOException {//robot 1 movement
        String[] parts = line.split(" ");
        //机器人读取输入流
        robot1.setItem(Integer.parseInt(parts[1]));//携带物品
        robot1.setDirection(Double.parseDouble(parts[7]));//朝向
        robot1.setPosition_x(Double.parseDouble(parts[8]));//机器人位置
        robot1.setPosition_y(Double.parseDouble(parts[9]));//机器人位置
//策略
        if(robot1.getTargetWorkBench()==-1&&robot1.getItem()==0){//机器人空闲 无目的地无携带
            int target_id;
            if(robot1.getTargetItem()==1){//找到最近的空闲的1
                target_id=calculateDistance(robot1,workbenches_1,1);//找出目的工作台id
                robot1.setTargetItem(2);//下次去取2
            }else{//找出最近的空闲的2
                target_id=calculateDistance(robot1,workbenches_2,2);//找出目的工作台id
                robot1.setTargetItem(1);//下次去取1
            }
            double target_x=integerworkbenchHashMap.get(target_id).getX();
            double target_y=integerworkbenchHashMap.get(target_id).getY();
            robot1.setTargetWorkBench_x(target_x);//目的地x
            robot1.setTargetWorkBench_y(target_y);//目的地y
            robot1.setTargetWorkBench(target_id);//设为目的地


            // find targetItem
        }else if(robot1.getTargetWorkBench()==-1&&robot1.getItem()!=0){//机器人携带了物品，但没有目的地
            //set target
            int target_id=calculateDistance(robot1,workbenches_4,4);//去找4如果没有就去找9
            double target_x=integerworkbenchHashMap.get(target_id).getX();
            double target_y=integerworkbenchHashMap.get(target_id).getY();
            robot1.setTargetWorkBench_x(target_x);//目的地x
            robot1.setTargetWorkBench_y(target_y);//目的地y
            robot1.setTargetWorkBench(target_id);//设为目的地

        }else if(robot1.getTargetWorkBench()!=-1&&robot1.getItem()==0){//机器人未携带物品 但有目的地--去取成品
            //detect whether reach des
            if(Integer.parseInt(parts[0])==robot1.getTargetWorkBench()){//如果到了目的地
                outStream.printf("buy %d\n", 0);//buy
                integerworkbenchHashMap.get(robot1.getTargetWorkBench()).setAsTarget(false);//工作台取消锁定
                robot1.setTargetWorkBench(-1);//目的地设置为空
            }
        }else{//机器人携带了物品 并且有目的地，将携带物品放入目的地的原材料格
            if(Integer.parseInt(parts[0])==robot1.getTargetWorkBench()){
                outStream.printf("sell %d\n", 0);//buy
                integerworkbenchHashMap.get(robot1.getTargetWorkBench()).setAsTarget(false);//工作台取消锁定
                robot1.setTargetWorkBench(-1);//目的地设置为空
            }

        }
//        robot1.setTargetWorkBench_x(workbenches_1.get(0).getX());//设定目的地位置
//        robot1.setTargetWorkBench_y(workbenches_1.get(0).getY());//设定目的地位置
        robot1.setAngular_velocity();
        outStream.printf("rotate %d %f\n", 0, robot1.getAngular_velocity());
        outStream.printf("forward %d %f\n", 0, robot1.getVelocity());


    }

    private static void robot2Move(robot robot2,String line) throws IOException {//robot 2 movement
        String[] parts = line.split(" ");
        //机器人读取输入流
        robot2.setItem(Integer.parseInt(parts[1]));//携带物品
        robot2.setDirection(Double.parseDouble(parts[7]));//朝向
        robot2.setPosition_x(Double.parseDouble(parts[8]));//机器人位置
        robot2.setPosition_y(Double.parseDouble(parts[9]));//机器人位置
        if(robot2.getTargetWorkBench()==-1&&robot2.getItem()==0){//机器人空闲 无目的地无携带
            int target_id;
            if(robot2.getTargetItem()==3){//找到最近的空闲的3
                target_id=calculateDistance(robot2,workbenches_3,3);//找出目的工作台id
                robot2.setTargetItem(1);//下次去取1
            }else{//找出最近的空闲的3
                target_id=calculateDistance(robot2,workbenches_1,1);//找出目的工作台id
                robot2.setTargetItem(3);//下次去取3
            }
            double target_x=integerworkbenchHashMap.get(target_id).getX();
            double target_y=integerworkbenchHashMap.get(target_id).getY();
            robot2.setTargetWorkBench_x(target_x);//目的地x
            robot2.setTargetWorkBench_y(target_y);//目的地y
            robot2.setTargetWorkBench(target_id);//设为目的地
        } else if(robot2.getTargetWorkBench()==-1&&robot2.getItem()!=0){//机器人携带了物品，但没有目的地
            //set target
            int target_id=calculateDistance(robot2,workbenches_5,5);//去找5如果没有就去找9
            double target_x=integerworkbenchHashMap.get(target_id).getX();
            double target_y=integerworkbenchHashMap.get(target_id).getY();
            robot2.setTargetWorkBench_x(target_x);//目的地x
            robot2.setTargetWorkBench_y(target_y);//目的地y
            robot2.setTargetWorkBench(target_id);//设为目的地

        }else if(robot2.getTargetWorkBench()!=-1&&robot2.getItem()==0){//机器人未携带物品 但有目的地--去取成品
            //detect whether reach des
            if(Integer.parseInt(parts[0])==robot2.getTargetWorkBench()){//如果到了目的地
                outStream.printf("buy %d\n", 1);//buy
                integerworkbenchHashMap.get(robot2.getTargetWorkBench()).setAsTarget(false);//工作台取消锁定
                robot2.setTargetWorkBench(-1);//目的地设置为空
            }
        }else{//机器人携带了物品 并且有目的地，将携带物品放入目的地的原材料格
            if(Integer.parseInt(parts[0])==robot2.getTargetWorkBench()){
                outStream.printf("sell %d\n", 1);//buy
                integerworkbenchHashMap.get(robot2.getTargetWorkBench()).setAsTarget(false);//工作台取消锁定
                robot2.setTargetWorkBench(-1);//目的地设置为空
            }

        }
//        robot1.setTargetWorkBench_x(workbenches_1.get(0).getX());//设定目的地位置
//        robot1.setTargetWorkBench_y(workbenches_1.get(0).getY());//设定目的地位置
        robot2.setAngular_velocity();
        outStream.printf("rotate %d %f\n", 1, robot2.getAngular_velocity());
        outStream.printf("forward %d %f\n", 1, robot2.getVelocity());
    }

    private static void robot3Move(robot robot3,String line) throws IOException {//robot 3 movement
        String[] parts = line.split(" ");
        //机器人读取输入流
        robot3.setItem(Integer.parseInt(parts[1]));//携带物品
        robot3.setDirection(Double.parseDouble(parts[7]));//朝向
        robot3.setPosition_x(Double.parseDouble(parts[8]));//机器人位置
        robot3.setPosition_y(Double.parseDouble(parts[9]));//机器人位置
        if(robot3.getTargetWorkBench()==-1&&robot3.getItem()==0){//机器人空闲 无目的地无携带
            int target_id;
            if(robot3.getTargetItem()==2){//找到最近的空闲的2
                target_id=calculateDistance(robot3,workbenches_2,2);//找出目的工作台id
                robot3.setTargetItem(3);//下次去取3
            }else{//找出最近的空闲的3
                target_id=calculateDistance(robot3,workbenches_3,3);//找出目的工作台id
                robot3.setTargetItem(2);//下次去取2
            }
            double target_x=integerworkbenchHashMap.get(target_id).getX();
            double target_y=integerworkbenchHashMap.get(target_id).getY();
            robot3.setTargetWorkBench_x(target_x);//目的地x
            robot3.setTargetWorkBench_y(target_y);//目的地y
            robot3.setTargetWorkBench(target_id);//设为目的地
        } else if(robot3.getTargetWorkBench()==-1&&robot3.getItem()!=0){//机器人携带了物品，但没有目的地
            //set target
            int target_id=calculateDistance(robot3,workbenches_6,6);//去找5如果没有就去找9
            double target_x=integerworkbenchHashMap.get(target_id).getX();
            double target_y=integerworkbenchHashMap.get(target_id).getY();
            robot3.setTargetWorkBench_x(target_x);//目的地x
            robot3.setTargetWorkBench_y(target_y);//目的地y
            robot3.setTargetWorkBench(target_id);//设为目的地

        }else if(robot3.getTargetWorkBench()!=-1&&robot3.getItem()==0){//机器人未携带物品 但有目的地--去取成品
            //detect whether reach des
            if(Integer.parseInt(parts[0])==robot3.getTargetWorkBench()){//如果到了目的地
                outStream.printf("buy %d\n", 2);//buy
                integerworkbenchHashMap.get(robot3.getTargetWorkBench()).setAsTarget(false);//工作台取消锁定
                robot3.setTargetWorkBench(-1);//目的地设置为空
            }
        }else{//机器人携带了物品 并且有目的地，将携带物品放入目的地的原材料格
            if(Integer.parseInt(parts[0])==robot3.getTargetWorkBench()){
                outStream.printf("sell %d\n", 2);//buy
                integerworkbenchHashMap.get(robot3.getTargetWorkBench()).setAsTarget(false);//工作台取消锁定
                robot3.setTargetWorkBench(-1);//目的地设置为空
            }

        }
//        robot1.setTargetWorkBench_x(workbenches_1.get(0).getX());//设定目的地位置
//        robot1.setTargetWorkBench_y(workbenches_1.get(0).getY());//设定目的地位置
        robot3.setAngular_velocity();
        outStream.printf("rotate %d %f\n", 2, robot3.getAngular_velocity());
        outStream.printf("forward %d %f\n", 2, robot3.getVelocity());

    }

    private static void robot4Move(robot robot4,String line) throws IOException {//robot 4 movement
        String[] parts = line.split(" ");
        //机器人读取输入流
        robot4.setItem(Integer.parseInt(parts[1]));//携带物品
        robot4.setDirection(Double.parseDouble(parts[7]));//朝向
        robot4.setPosition_x(Double.parseDouble(parts[8]));//机器人位置
        robot4.setPosition_y(Double.parseDouble(parts[9]));//机器人位置
        if(robot4.getTargetWorkBench()==-1&&robot4.getItem()==0) {//机器人空闲 无目的地无携带
            int target_id=find4Workbench(7,robot4);//先看有没有7
            if(target_id!=-1){
                robot4.setTargetItem(7);
            }else if(robot4.getTargetItem()==6){
                target_id=find4Workbench(6, robot4);
                robot4.setTargetItem(5);//下次取5
                if(target_id==-1) {
                    target_id=calculateDistance(robot4, workbenches_3,3);//如果没有456产出 找3
                    robot4.setTargetItem(3);
                }
            }else if(robot4.getTargetItem()==5){
                target_id=find4Workbench(5, robot4);
                robot4.setTargetItem(4);//下次取4
                if(target_id==-1) {
                    target_id=calculateDistance(robot4, workbenches_2,2);//如果没有456产出 找2
                    robot4.setTargetItem(2);
                }
            }else{
                target_id=find4Workbench(4, robot4);
                robot4.setTargetItem(6);//下次取6
                if(target_id==-1) {
                    target_id=calculateDistance(robot4, workbenches_1,1);//如果没有456产出 找1
                    robot4.setTargetItem(1);
                }
            }
            double target_x=integerworkbenchHashMap.get(target_id).getX();
            double target_y=integerworkbenchHashMap.get(target_id).getY();
            robot4.setTargetWorkBench_x(target_x);//目的地x
            robot4.setTargetWorkBench_y(target_y);//目的地y
            robot4.setTargetWorkBench(target_id);//设为目的地
        }else if(robot4.getTargetWorkBench()==-1&&robot4.getItem()!=0) {//机器人携带了物品，但没有目的地
            int item=robot4.getItem();
            int target_id=1;
            if(item==1){
                target_id=calculateDistance(robot4,workbenches_4,4);//去找4如果没有就去找9
            }else if(item==2){
                target_id=calculateDistance(robot4,workbenches_5,5);//去找5如果没有就去找9
            }else if(item==3){
                target_id=calculateDistance(robot4,workbenches_6,6);//去找6如果没有就去找9
            }else if(item==4||item==5||item==6){
                target_id=calculateDistance(robot4,workbenches_7,7);//去找7如果没有就去找9
            }else {//带了7
                target_id=calculateDistance(robot4,workbenches_8,8);//去找7如果没有就去找9
            }
            double target_x=integerworkbenchHashMap.get(target_id).getX();
            double target_y=integerworkbenchHashMap.get(target_id).getY();
//            integerworkbenchHashMap.get()
            robot4.setTargetWorkBench_x(target_x);//目的地x
            robot4.setTargetWorkBench_y(target_y);//目的地y
            robot4.setTargetWorkBench(target_id);//设为目的地
        }else if(robot4.getTargetWorkBench()!=-1&&robot4.getItem()==0){//机器人未携带物品 但有目的地--去取成品
            if(Integer.parseInt(parts[0])==robot4.getTargetWorkBench()){//如果到了目的地
                outStream.printf("buy %d\n", 3);//buy
                integerworkbenchHashMap.get(robot4.getTargetWorkBench()).setAsTarget(false);//工作台取消锁定
                robot4.setTargetWorkBench(-1);//目的地设置为空
            }

        }else{//机器人携带了物品 并且有目的地，将携带物品放入目的地的原材料格
            if(Integer.parseInt(parts[0])==robot4.getTargetWorkBench()){
                outStream.printf("sell %d\n", 3);//卖
                integerworkbenchHashMap.get(robot4.getTargetWorkBench()).setAsTarget(false);//工作台取消锁定
                robot4.setTargetWorkBench(-1);//目的地设置为空
            }

        }
//        robot1.setTargetWorkBench_x(workbenches_1.get(0).getX());//设定目的地位置
//        robot1.setTargetWorkBench_y(workbenches_1.get(0).getY());//设定目的地位置
        robot4.setAngular_velocity();
        outStream.printf("rotate %d %f\n", 3, robot4.getAngular_velocity());
        outStream.printf("forward %d %f\n", 3, robot4.getVelocity());

    }
//求机器人与工作台的最短距离
    //默认一定存在123号工作台
    private static int calculateDistance(robot r, ArrayList<workbench> arrayList,int type) throws IOException {

        int number= arrayList.size();//某类型工作台数量
        if(number==0){//如果非123号工作台不存在
//            isWriter.write("bbbb");
            if(type==4||type==5||type==6||type==8) return calculateDistance(r,workbenches_9,9);//4或者5或者6号不存在去找9
        }
        int id_free=-1;//最近的未被设为目标的工作台
        int id_all=-1;//最近的工作台
        double dis=9999999;//最近的距离
        double dis_all=999999;//全局最近距离
        for (workbench t:arrayList) {
//            if((type==1||type==2||type==3)&&t.getProductStatus()==0){//取原材料
//                continue;
//            }else if(type==4||type==5||type==6){//去放原材料
////                if()
//            }

            double r_x=r.getPosition_x();
            double r_y = r.getPosition_y();
            double t_x=t.getX();
            double t_y =t.getY();
            double cur_dis=pow(r_x-t_x,2)+pow(r_y-t_y, 2);
            if(cur_dis<dis){
                ArrayList<Integer> workbenchMa= getWorkbenchItem(t);//获得工作台已经有的原材料
                int item=r.getItem();//机器人携带的物品
                if(item ==1||item==2||item==3||item==4||item==5||item==6){//如果是携带了原材料123或者456
                    if(!workbenchMa.contains(item)&&!t.isAsTarget()){//如果工作台没有机器人携带的原材料,且工作台没有被作为目标
                        id_free = t.getID();//设为目标
//                        isWriter.write("aaaaaa"+id_free);
                        dis=cur_dis;
                    }
                } else if (item==0) {//没有携带目物品
                    if(!t.isAsTarget()){//当前工作台没有被作为目标
                        id_free = t.getID();//设为目标
                        dis=cur_dis;
                    }
                }else{//携带7号物品
                    id_free = t.getID();//设为目标
                    dis=cur_dis;
                }
                if(cur_dis<dis_all){
                    id_all=t.getID();//全局
                    dis_all=cur_dis;
                }


            }
        }
        if(id_free==-1){//没有任何一个工作台空闲
//            isWriter.write("qqq");
            return id_all;//返回最近的工作台
        }else{//存在空闲的工作台
            integerworkbenchHashMap.get(id_free).setAsTarget(true);//设为目标
//            isWriter.write("ppp");
            if(type==8&&type==9) integerworkbenchHashMap.get(id_free).setAsTarget(false);// 89 号工作台可以瞬间卖出 故不设置为非空闲
            return id_free;
        }

    }
    public static int find4Workbench(int type,robot robot4){
        int targetID=-1;
        double dis=999999;
        if(type==7){if(workbenches_7.size()!=0) for (workbench t:workbenches_7) {
            double x=t.getX();
            double y=t.getY();
            double cur_dis=pow(robot4.getPosition_x()-x,2)+pow(robot4.getPosition_y()-y, 2);
            if(cur_dis<dis&&t.getProductStatus()==1){
                dis=cur_dis;
                targetID=t.getID();
                robot4.setTargetItem(7);
            }
        }


        }
        if(type==6){
            if(workbenches_6.size()!=0) for (workbench t:workbenches_6) {
                double x=t.getX();
                double y=t.getY();
                double cur_dis=pow(robot4.getPosition_x()-x,2)+pow(robot4.getPosition_y()-y, 2);
                if(cur_dis<dis&&t.getProductStatus()==1){
                    dis=cur_dis;
                    targetID=t.getID();
                    robot4.setTargetItem(6);
                }
            }
            if(targetID==-1)return find4Workbench(5,robot4);
        }



        if(type==5){
            if(workbenches_5.size()!=0) for (workbench t:workbenches_5){
                double x=t.getX();
                double y=t.getY();
                double cur_dis=pow(robot4.getPosition_x()-x,2)+pow(robot4.getPosition_y()-y, 2);
                if(cur_dis<dis&&t.getProductStatus()==1){
                    dis=cur_dis;
                    targetID=t.getID();
                    robot4.setTargetItem(5);
                }
            }
            find4Workbench(4,robot4);
        }

        if(type==4){
            if(workbenches_4.size()!=0) for (workbench t:workbenches_4){
                double x=t.getX();
                double y=t.getY();
                double cur_dis=pow(robot4.getPosition_x()-x,2)+pow(robot4.getPosition_y()-y, 2);
                if(cur_dis<dis&&t.getProductStatus()==1){
                    dis=cur_dis;
                    targetID=t.getID();
                    robot4.setTargetItem(4);
                }

            }
        }
        return targetID;
    }
    public static ArrayList<Integer> getWorkbenchItem(workbench t){//获得工作台已有的原材料
        ArrayList<Integer> ans= new ArrayList<>();
        String num=  Integer.toBinaryString(t.getMaterialStatus()).toString();
        int k=0;//物品类型
        for(int i =num.length()-1;i>=0;i--){
            int itemN = Integer.parseInt(String.valueOf(num.charAt(i)));
            if(itemN !=0){
                ans.add(k);
            }
            k++;
        }
        return ans;
    }

    private static boolean readUtilOK() throws IOException {//读取输入流
        String line;
        int workbenchInfoNumber=0;//计数工作台信息行数
        int robotInfoNumber=1;//计数机器人信息行数  1表示第一个机器人信息
        if(inStream.hasNextLine()){//每一帧第二行：获取工作台数量
            line = inStream.nextLine();
//            readInStream(line, 0);
//            workbench_number=Integer.parseInt(line);
//            if(!workbenchInit){//如果工作台信息没有被初始化
//                workbenchArr= new workbench[workbench_number];
//                workbenchInit=true;
//            }
        }
        while (inStream.hasNextLine()) {//获取除第一 二行外的其他行
            line = inStream.nextLine();
           if(workbenchInfoNumber<workbench_number){//如果是工作台信息
                //do something
               //parts[0] :工作台类型 [1,9] 整数
//               parts[1] ,parts[2]:工作台坐标x y  浮点
//               parts[3] 剩余时间（帧） 整数  -1：表示没有生产。 0：表示生产因输出格满而阻塞。  >=0：表示剩余生产帧数。
//               parts[4] 原材料状态二进制位表描述，例如 48(110000)表示拥有物品 4 和 5。
//               parts[5]  0：表示无。 1：表示有。
               String[] parts = line.split(" ");
               workbench tem=integerworkbenchHashMap.get(workbenchInfoNumber);//获得该行对应的工作台信息
               tem.setRemainingTime(Integer.parseInt(parts[3]));//设置剩余时间
               tem.setMaterialStatus(Integer.parseInt(parts[4]));//设置原材料状态
               tem.setProductStatus(Integer.parseInt(parts[5]));//设置产品格状态
//               isWriter.write("编号："+workbenchInfoNumber+"原材料状态："
//                       +integerworkbenchHashMap.get(workbenchInfoNumber).getMaterialStatus()
//                       +integerworkbenchHashMap.get(workbenchInfoNumber).isAsTarget()
//                       +"  ");
                workbenchInfoNumber++;
            }else if(robotInfoNumber==1){//获取机器人1的信息，并设定其运动轨迹
                robot1Move(robot1,line);
                robotInfoNumber++;
            }else if(robotInfoNumber==2){//获取机器人2的信息，并设定其运动轨迹
               robot2Move(robot2,line);
               robotInfoNumber++;
           }else if(robotInfoNumber==3){//获取机器人的信息，并设定其运动轨迹
               robot3Move(robot3,line);
               robotInfoNumber++;
           }else if(robotInfoNumber==4){//获取机器人4的信息，并设定其运动轨迹
               robot4Move(robot4,line);
               robotInfoNumber++;
           }
//            readInStream(line, 0);//读取每一帧除第一行以外的所有行，参数0无意义
            if ("OK".equals(line)) {//帧结束
                return true;
            }

        }
//        outStream.print("OK\n");//读每一帧结束ok
//        outStream.flush();
        return false;
    }
    private static Boolean readMap() {//初始化地图 5s
        robot1.setTargetItem(1);//初始话机器人1的目标为1号物品
        robot2.setTargetItem(3);//初始话机器人2的目标为3号物品
        robot3.setTargetItem(2);//初始化机器人3的目标为2号物品
        robot4.setTargetItem(6);//初始化机器人6的目标为2号物品
        BufferedWriter bw;

//            try {
//                mapFile.createNewFile();
//                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(mapFile, true)));
                String mapLine;
                int i = 0;//行数 记录地图到了第几行
                while (inStream.hasNextLine()) {
                    mapLine = inStream.nextLine();
                    if ("OK".equals(mapLine)) {
//                        bw.flush();
//                        bw.close();
                        return true;
                    }
//                    bw.write(mapLine);
//                    bw.write("\r\n");
                    init(mapLine, i);
                    i++;
                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        //将map写到文件
        return false;
    }

    public static void init(String mapLine, int i) {//地图初始化
        for (int j = 0; j < 100; j++) {
            char tem = mapLine.charAt(j);
            if(tem=='.'){
            } else if (tem=='A') {
                robot_x[robot_number] = j / 2.0 + 0.25;
                robot_y[robot_number] = (100 - i) / 2.0 - 0.25;
                robot_number++;
            }else{
                initMapAndArr(tem,i,j);
            }
            map[i][j] = tem;
        }
    }

    public static  void initMapAndArr(char typeChar,int i,int j){//初始化map 和arraylist
        workbench temWorkbench =new workbench();
        int type=Character.getNumericValue(typeChar);
        temWorkbench.setID(workbench_number);
        temWorkbench.setX(j / 2.0 + 0.25);
        temWorkbench.setY((100 - i) / 2.0 - 0.25);
        temWorkbench.setType(type);
        temWorkbench.setAsTarget(false);
        integerworkbenchHashMap.put(workbench_number, temWorkbench);
        workbenchArr.add(temWorkbench);
        switch (typeChar){
            case '1':
                workbench1_x.add(j / 2.0 + 0.25);
                workbench1_y.add((100 - i) / 2.0 - 0.25);
                workbenches_1.add(temWorkbench);
                break;
            case '2':
                workbench2_x.add(j / 2.0 + 0.25);
                workbench2_y.add((100 - i) / 2.0 - 0.25);
                workbenches_2.add(temWorkbench);
                break;
            case '3':
                workbench3_x.add(j / 2.0 + 0.25);
                workbench3_y.add((100 - i) / 2.0 - 0.25);
                workbenches_3.add(temWorkbench);
                break;
            case '4':
                workbench4_x.add(j / 2.0 + 0.25);
                workbench4_y.add((100 - i) / 2.0 - 0.25);
                workbenches_4.add(temWorkbench);
                break;
            case '5':
                workbench5_x.add(j / 2.0 + 0.25);
                workbench5_y.add((100 - i) / 2.0 - 0.25);
                workbenches_5.add(temWorkbench);
                break;
            case '6':
                workbench6_x.add(j / 2.0 + 0.25);
                workbench6_y.add((100 - i) / 2.0 - 0.25);
                workbenches_6.add(temWorkbench);
                break;
            case '7':
                workbench7_x.add(j / 2.0 + 0.25);
                workbench7_y.add((100 - i) / 2.0 - 0.25);
                workbenches_7.add(temWorkbench);
                break;
            case '8':
                workbench8_x.add(j / 2.0 + 0.25);
                workbench8_y.add((100 - i) / 2.0 - 0.25);
                workbenches_8.add(temWorkbench);
                break;
            case '9':
                workbench9_x.add(j / 2.0 + 0.25);
                workbench9_y.add((100 - i) / 2.0 - 0.25);
                workbenches_9.add(temWorkbench);
                break;
        }
        workbench_number++;

    }

//    public static void isWriterInit() {//输入流写初始化
//        try {
//            if (!isFile.exists()) {
//                isFile.createNewFile();
//                isWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(isFile, true)));
//            }
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
//    }

//    public static void readInStream(String line, int frameID) {//读输入流并写入文件
//        try {
//            if(frameID==9000) frameFlag=true;
//            isWriter.write(line);
//            isWriter.write("\r\n");
//            isWriter.flush();
//            if ("OK".equals(line)&&frameFlag) {
//                isWriter.write((robot1.getTargetWorkBench_x())+" ");
//                isWriter.write(Double.toString(robot1.getTargetWorkBench_y()));
//                isWriter.flush();
//                isWriter.close();
//            }
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
}
class robot {
    private double direction;//机器人朝向=acos(velocity_x/sqrt(velocity_x*velocity_x+velocity_y*velocity_y))
    private double velocity_x;//线速度x分量
    private double velocity_y;//线速度y分量
    private double lastVel=0;//上一时刻与工作站的角度



    private int targetItem;//想要携带的物品类型  机器人1  1 2 号物品轮流作为目标

    public double getLabel() {
        return label;
    }

    public void setLabel(double label) {
        this.label = label;
    }

    private double velocity;//速度



    //0 未携带 [1,7] 对应类型
    private int item=0;//携带物品类型
    private double label=1;//角速度正负

    private int targetWorkBench=-1;//目的地的工作台id
    private double targetWorkBench_x;//目的工作台位置x
    private double targetWorkBench_y;//目的工作台位置y
    private double angular_velocity;//要设定的角速度
    private double position_x;//位置横坐标
    private double position_y;//位置纵坐标

    public int getTargetItem() {return targetItem;}

    public void setTargetItem(int targetItem) {this.targetItem = targetItem;}
    public double getDirection() {return direction;}


    public void setDirection(double direction) {this.direction = direction;}

    public double getPosition_x() {return position_x;}

    public void setPosition_x(double position_x) {this.position_x = position_x;}

    public double getPosition_y() {return position_y;}

    public void setPosition_y(double position_y) {this.position_y = position_y;}

    public int getTargetWorkBench() {return targetWorkBench;}

    public double getTargetWorkBench_x() {return targetWorkBench_x;}

    public void setTargetWorkBench_x(double targetWorkBench_x) {this.targetWorkBench_x = targetWorkBench_x;}

    public double getTargetWorkBench_y() {return targetWorkBench_y;}

    public void setTargetWorkBench_y(double targetWorkBench_y) {this.targetWorkBench_y = targetWorkBench_y;}

    public double getAngular_velocity() {return angular_velocity;}

    public void setItem(int item) {this.item = item;}
    public int getItem() {return item;}

    public void setTargetWorkBench(int targetWorkBench) {this.targetWorkBench = targetWorkBench;}

    public void setDirection(Double direction) {this.direction = direction;}

    public double getVelocity_value() {return sqrt(pow(velocity_x, 2) + pow(velocity_y, 2));}
    public void setVelocity(double velocity) {this.velocity = velocity;}
    public double getVelocity() {return velocity;}

    public void setAngular_velocity() throws IOException {
//        setVelocity(1);
        //期望朝向
        double b_x = getTargetWorkBench_x() - getPosition_x();//目标朝向向量横坐标
        double b_y= getTargetWorkBench_y()-getPosition_y();//目标朝向向量纵坐标
        double dis=sqrt(pow(b_x,2)+pow(b_y,2));
        double label=b_y>0.0?1.0:-1.0;
        double Alpha=acos(b_x/dis)*label;//期望朝向



        //角速度设定计算
        label =Alpha-getDirection()>0.0?1.0:-1.0;//角速度正负 1.0--逆时针 -1.0 顺时针
        this.angular_velocity=calLabel(Alpha,getDirection())*calVal(Alpha,getDirection())*5;

        //根据角度设定线速度
        if(abs(angular_velocity)>=0.2&&abs(lastVel-calVal(Alpha,getDirection()))<=0.1&&getVelocity()>0.5){
            setVelocity(max(getVelocity()-0.1,0));
        }else if(calVal(Alpha,getDirection())>=(PI/2.0+0.5)){
            if(dis<=2&&calVal(Alpha,getDirection())>=0.1) setVelocity(-0.5);
            else
                setVelocity(-2);
        }else{
//            if(dis<=2&&calVal(Alpha,getDirection())>=0.1) setVelocity(0.9);
//            else
                setVelocity(6);

        }
        lastVel=calVal(Alpha,getDirection());//获得上一帧的朝向差
//        isWriter.write("目的地："+getTargetWorkBench()
//                +" 目的地坐标 x:"+getTargetWorkBench_x()+" y:"+getTargetWorkBench_y()
//                +" 机器人x:"+getPosition_x()+" 机器人y: "+getPosition_y()
//                +" 目的地工作台属性 "+(getTargetWorkBench()==-1?"空":integerworkbenchHashMap.get(getTargetWorkBench()).getType()+"")
//                + " 朝向："+getDirection()
//                + " Alpha "+Alpha
//                +" calVal:"+calVal(Alpha,getDirection())
//                +"\n"


//        );
    }
    public double calLabel(double Alpha,double Direction ){//计算方向
        if(Direction>=0){
            if(Alpha>=0) return signum(Alpha-Direction);
            else return abs(Alpha)+Direction>PI?1:-1;

        }else {
            if(Alpha>=0) return abs(Direction)+Alpha>PI?-1:1;
            else return Alpha-Direction<0?-1:1;
        }
    }
    public double calVal(double Alpha,double Direction){//计算角速度大小
        if(Direction>=0){
            if(Alpha>=0) return abs(Alpha-Direction);
            else return min(abs(Alpha)+Direction,2*PI-(abs(Alpha)+Direction));
        }else {
            if(Alpha>=0) return min(abs(Direction)+Alpha,2*PI-(abs(Direction)+Alpha));
            else return  abs(Alpha-Direction);
        }
//        return abs(Alpha-Direction);
    }
    public int dem(double n){//判断处于第几象限
        if(n>=0&&n<=PI/2){
            return 1;
        }else if(n>PI/2&&n<=PI){
            return 2;
        }else if(n<=0&&n>=-PI/2){
            return 4;
        }else {
            return 3;
        }

    }
}
class workbench{
    private int type;//工作台类型
    private int ID;
    private double x;//位置
    private double y;//位置
    private int remainingTime;//剩余生产时间（帧）
    private int materialStatus;//原材料状态



    private int productStatus;//产品格状态
    private boolean asTarget=false;//是否被设置为目标

    public boolean isAsTarget() {return asTarget;}

    public void setAsTarget(boolean asTarget) {this.asTarget = asTarget;}


    public int getType() {return type;}

    public void setType(int type) {this.type = type;}
    public int getID() {return ID;}

    public void setID(int ID) {this.ID = ID;}

    public double getX() {return x;}

    public void setX(double x) {this.x = x;}

    public double getY() {return y;}

    public void setY(double y) {this.y = y;}

    public int getRemainingTime() {return remainingTime;}

    public void setRemainingTime(int remainingTime) {this.remainingTime = remainingTime;}

    public int getMaterialStatus() {return materialStatus;}

    public void setMaterialStatus(int materialStatus) {this.materialStatus = materialStatus;}

    public int isProductStatus() {return productStatus;}

    public void setProductStatus(int productStatus) {this.productStatus = productStatus;}
    public int getProductStatus() {return productStatus;}

}
